import QtQuick 
import QtQuick.Controls  
import QtQuick.Layouts  
import QtQuick.Window  
import TSCore.Presentation 
import "components"
import "styles" as Styles

Rectangle {
    id: appShell
    anchors.fill: parent

    // 窗口相关属性 - 不再需要ViewModels属性！
    property bool isMaximized: false  // 从main.qml传入的窗口状态
    property bool isOnTop: false      // 从main.qml传入的置顶状态

    // 窗口控制信号
    signal windowMinimize()
    signal windowMaximize()
    signal windowRestore()
    signal windowClose()
    signal windowToggleOnTop()
    signal windowStartDrag()  // 新增拖拽信号

    color: Styles.Theme.surface  // 使用与标题栏相同的颜色，避免白边

    // 主布局
    ColumnLayout {
        anchors.fill: parent
        
        // 顶部标题栏
        Rectangle {
            id: titleBar
            Layout.fillWidth: true
            Layout.preferredHeight: 40  // 从60减少到40
            color: Styles.Theme.surface
            z: 1001

            // 渐变背景
            gradient: Gradient {
                GradientStop { position: 0.0; color: Styles.Theme.surface }
                GradientStop { position: 1.0; color: Qt.darker(Styles.Theme.surface, 1.1) }
            }

            // 标题栏内容
            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: Styles.Theme.spacingNormal
                anchors.rightMargin: Styles.Theme.spacingNormal
                spacing: Styles.Theme.spacingLarge

                // 左侧 - 系统标题
                Text {
                    text: qsTr("AGV调度系统")
                    color: Styles.Theme.textPrimary
                    font.pixelSize: Styles.Theme.fontSizeMedium
                    font.weight: Font.Bold
                    Layout.alignment: Qt.AlignVCenter
                }

                // 中间可拖拽区域（弹性空间）
                Item {
                    id: titleBarDragArea
                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    // 标题栏拖拽处理器 - 只在标题栏的空白区域生效
                    DragHandler {
                        id: titleBarDragHandler
                        grabPermissions: TapHandler.CanTakeOverFromAnything
                        target: null
                        onActiveChanged: {
                            if (active) {
                                appShell.windowStartDrag()
                            }
                        }
                    }

                    // 双击最大化/还原
                    TapHandler {
                        acceptedButtons: Qt.LeftButton
                        onDoubleTapped: appShell.windowMaximize()
                    }
                }

                // 右侧 - 窗口控制按钮
                Row {
                    Layout.alignment: Qt.AlignVCenter

                    // 置顶按钮
                    Rectangle {
                        width: 28
                        height: 28
                        color: pinMouseArea.containsMouse ? Styles.Theme.surfaceVariant :
                               (isOnTop ? Qt.alpha(Styles.Theme.primary, 0.2) : "transparent")
                        radius: 4
                        border.width: isOnTop ? 1 : 0
                        border.color: isOnTop ? Styles.Theme.primary : "transparent"

                        Image {
                            anchors.centerIn: parent
                            width: 14
                            height: 14
                            source: isOnTop ? "qrc:/icons/window-bar/pin-fill.svg" : "qrc:/icons/window-bar/pin.svg"
                            fillMode: Image.PreserveAspectFit
                        }

                        MouseArea {
                            id: pinMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: appShell.windowToggleOnTop()
                        }

                        ToolTip {
                            visible: pinMouseArea.containsMouse
                            text: isOnTop ? qsTr("取消置顶") : qsTr("窗口置顶")
                            delay: 500
                        }
                    }

                    // 最小化按钮
                    Rectangle {
                        width: 28
                        height: 28
                        color: minimizeMouseArea.containsMouse ? Styles.Theme.surfaceVariant : "transparent"
                        radius: 4

                        Image {
                            anchors.centerIn: parent
                            width: 14
                            height: 14
                            source: "qrc:/icons/window-bar/minimize.svg"
                            fillMode: Image.PreserveAspectFit
                        }

                        MouseArea {
                            id: minimizeMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: appShell.windowMinimize()
                        }

                        ToolTip {
                            visible: minimizeMouseArea.containsMouse
                            text: qsTr("最小化")
                            delay: 500
                        }
                    }

                    // 最大化/还原按钮
                    Rectangle {
                        width: 28
                        height: 28
                        color: maximizeMouseArea.containsMouse ? Styles.Theme.surfaceVariant : "transparent"
                        radius: 4

                        Image {
                            anchors.centerIn: parent
                            width: 14
                            height: 14
                            source: isMaximized ? "qrc:/icons/window-bar/restore.svg" : "qrc:/icons/window-bar/maximize.svg"
                            fillMode: Image.PreserveAspectFit
                        }

                        MouseArea {
                            id: maximizeMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: appShell.windowMaximize()
                        }

                        ToolTip {
                            visible: maximizeMouseArea.containsMouse
                            text: isMaximized ? qsTr("还原") : qsTr("最大化")
                            delay: 500
                        }
                    }

                    // 关闭按钮
                    Rectangle {
                        width: 28
                        height: 28
                        color: closeMouseArea.containsMouse ? "#E81123" : "transparent"
                        radius: 4

                        Image {
                            anchors.centerIn: parent
                            width: 14
                            height: 14
                            source: "qrc:/icons/window-bar/close.svg"
                            fillMode: Image.PreserveAspectFit
                        }

                        MouseArea {
                            id: closeMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: appShell.windowClose()
                        }

                        ToolTip {
                            visible: closeMouseArea.containsMouse
                            text: qsTr("关闭")
                            delay: 500
                        }
                    }
                }
            }


        }

        // 主内容区域
        Row {
            Layout.fillWidth: true
            Layout.fillHeight: true

            // 左侧导航栏
            Rectangle {
                id: navigationBar
                width: sideBarExpanded ? Styles.Theme.sideBarWidthExpanded : Styles.Theme.sideBarWidth
                height: parent.height
                color: Styles.Theme.surface

                property bool sideBarExpanded: false
                property int currentIndex: 0

                // 导航项数据模型
                property var navigationItems: [
                    { icon: "qrc:/icons/screen/dashboard.svg", text: qsTr("仪表盘"), screen: "dashboard" },
                    { icon: "qrc:/icons/screen/vehicles.svg", text: qsTr("车辆管理"), screen: "vehicles" },
                    { icon: "qrc:/icons/screen/tasks.svg", text: qsTr("任务管理"), screen: "tasks" },
                    { icon: "qrc:/icons/screen/analytics.svg", text: qsTr("数据统计"), screen: "analytics" },
                    { icon: "qrc:/icons/screen/simulator.svg", text: qsTr("仿真控制"), screen: "simulator" },
                    { icon: "qrc:/icons/screen/logs.svg", text: qsTr("日志中心"), screen: "logs" },
                    { icon: "qrc:/icons/screen/control.svg", text: qsTr("资源管理"), screen: "resources" },
                    { icon: "qrc:/icons/screen/settings.svg", text: qsTr("系统设置"), screen: "settings" }
                ]

                // 顶部：折叠按钮
                Rectangle {
                    id: toggleButton
                    anchors.top: parent.top
                    anchors.left: parent.left
                    anchors.right: parent.right
                    height: 48
                    color: toggleMouseArea.containsMouse ? Styles.Theme.surfaceVariant : "transparent"

                    Image {
                        anchors.centerIn: parent
                        source: navigationBar.sideBarExpanded ? "qrc:/icons/arrow-left.svg" : "qrc:/icons/arrow-right.svg"
                        width: 20
                        height: 20
                        fillMode: Image.PreserveAspectFit
                    }

                    MouseArea {
                        id: toggleMouseArea
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: navigationBar.sideBarExpanded = !navigationBar.sideBarExpanded
                    }
                }

                // 分隔线
                Rectangle {
                    anchors.top: toggleButton.bottom
                    anchors.left: parent.left
                    anchors.right: parent.right
                    height: 1
                    color: Styles.Theme.border
                }

                // 导航项列表
                ListView {
                    id: navigationList
                    anchors.top: toggleButton.bottom
                    anchors.topMargin: 1
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    model: navigationBar.navigationItems
                    currentIndex: navigationBar.currentIndex

                    delegate: Item {
                        width: navigationList.width
                        height: 48

                        Rectangle {
                            anchors.fill: parent
                            color: {
                                if (index === navigationBar.currentIndex) return Qt.alpha(Styles.Theme.primary, 0.1)
                                if (navMouseArea.containsMouse) return Styles.Theme.surfaceVariant
                                return "transparent"
                            }

                            // 左侧高亮条
                            Rectangle {
                                visible: index === navigationBar.currentIndex
                                anchors.left: parent.left
                                anchors.top: parent.top
                                anchors.bottom: parent.bottom
                                width: 3
                                color: Styles.Theme.primary
                            }

                            // 图标容器
                            Item {
                                anchors.centerIn: navigationBar.sideBarExpanded ? undefined : parent
                                anchors.left: navigationBar.sideBarExpanded ? parent.left : undefined
                                anchors.leftMargin: navigationBar.sideBarExpanded ? 16 : 0
                                anchors.verticalCenter: parent.verticalCenter
                                width: navigationBar.sideBarExpanded ? parent.width - 16 : 20
                                height: 20

                                Row {
                                    anchors.left: parent.left
                                    anchors.verticalCenter: parent.verticalCenter
                                    spacing: 12

                                    Image {
                                        source: modelData.icon
                                        width: 20
                                        height: 20
                                        fillMode: Image.PreserveAspectFit
                                        anchors.verticalCenter: parent.verticalCenter
                                    }

                                    Text {
                                        visible: navigationBar.sideBarExpanded
                                        text: modelData.text
                                        color: index === navigationBar.currentIndex ? Styles.Theme.primary : Styles.Theme.textPrimary
                                        font.pixelSize: Styles.Theme.fontSizeNormal
                                        font.weight: index === navigationBar.currentIndex ? Font.Medium : Font.Normal
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                }
                            }

                            MouseArea {
                                id: navMouseArea
                                anchors.fill: parent
                                hoverEnabled: true
                                onClicked: {
                                    navigationBar.currentIndex = index
                                    contentLoader.source = getScreenSource(modelData.screen)
                                }
                            }
                        }
                    }
                }

                // 右侧边框
                Rectangle {
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    anchors.right: parent.right
                    width: 1
                    color: Styles.Theme.border
                }

                Behavior on width {
                    NumberAnimation {
                        duration: Styles.Theme.animationNormal
                        easing.type: Easing.OutCubic
                    }
                }
            }

            // 右侧主内容区域
            Rectangle {
                width: parent.width - navigationBar.width
                height: parent.height
                color: Styles.Theme.background

                Loader {
                    id: contentLoader
                    anchors.fill: parent

                    // 不再需要复杂的ViewModel传递！
                    // 每个组件直接使用全局上下文属性
                }
            }
        }
    }

    // 辅助函数：根据屏幕名称获取对应的QML文件路径
    function getScreenSource(screenName) {
        switch (screenName) {
            case "dashboard": return "qrc:/qml/screens/DashboardScreen.qml"
            case "vehicles": return "qrc:/qml/screens/VehicleManagementScreen.qml"
            case "tasks": return "qrc:/qml/screens/TaskManagementScreen.qml"
            case "analytics": return "qrc:/qml/screens/AnalyticsScreen.qml"
            case "simulator": return "qrc:/qml/screens/SimulatorScreen.qml"
            case "logs": return "qrc:/qml/screens/LogCenterScreen.qml"
            case "resources": return "qrc:/qml/screens/ResourceManagementScreen.qml"
            case "settings": return "qrc:/qml/screens/SettingsScreen.qml"
            default: return "qrc:/qml/screens/DashboardScreen.qml"
        }
    }

    // 初始化：默认加载仪表盘
    Component.onCompleted: {
        contentLoader.source = getScreenSource("dashboard")
    }
}